/*
 * @Author: monet hanxu@huaun.com
 * @Date: 2023-09-25 11:24:03
 * @LastEditors: zhangyonggui
 * @LastEditTime: 2024-01-18 09:36:42
 * @FilePath: /h-radar-expose/plugins/mixins/detialMixin.js
 * @Description: 主要存储详情页中公共的内容
 */
import { ref ,reactive} from 'vue';
import { useStore } from 'vuex';
import { useRoute } from 'vue-router';
import {ElMessageBox} from 'element-plus'
export function usewebDetail () {
    const store = useStore();
    const route = useRoute();
    const category =ref('');
 const blockList = ref<any>([
  {
    title: '暴露风险评估',
    img: '',
    tip: '',
    list: [],
  },
  {
    title: '资产暴露程度评估',
    img: '',
    tip: '',
    list: [],
  },
  {
    title: '易受攻击性评估',
    img: '',
    tip: '',
    list: [],
  },
]);

    const detialInfo =ref<any>({});
    const basicInfo =ref<any>({});
    const changeList =ref([]);
    const id = ref(route.query.id || '');
    const AssetChanged =reactive<any>({
          category,
          dataId: id.value,
          pageNumber: 1,
          pageSize: 100,
          noPage: false,
    });
    /**
       * @description: 获取页面的基础信息
       * @param {*} path：store中的方法
       * @return {*}
       */
      const getDetialById=async(path)=> {
        const { code, data } = await store.dispatch(path, id.value);
        basicInfo.value = code === 0 ? data : {};
        detialInfo.value = code === 0 ? data : {};
      }
      /**
       * @description: 获取左侧信息
       * @return {*}
       */
      const getLeftBaseInfo=async()=> {
        const { code, data } = await store.dispatch('detail/getBaseInfo', {
            businessType: category.value,
            id: id.value,
          
        });
        basicInfo.value = code === 0 ? data : {};
      }
      /**
       * @description: 获取资产变更信息
       * @return {*}
       */
      const getAssetChanged=async()=> {
        AssetChanged.category = category.value;
        AssetChanged.dataId = id.value;
        const { code, data } = await store.dispatch(
          'detail/getAssetChanged',
          AssetChanged,
        );
        if (code === 0) {
          changeList.value = changeList.value.concat(data.list);
  
          if (data.list.length < 10) {
            AssetChanged.noPage = true;
          }
        }
      }
      /**
       * @description: 左侧标签操作：删除标签
       * @param {*} tag
       * @return {*}
       */
      const delTag=async(tag)=> {
        const data = {
          exposedCode: category.value,
          assetId: id.value,
          tag,
        };
        const { code } = await store.dispatch('api/deleteTags', data);
        if (code === 0) {
         basicInfo.value.tag = basicInfo.value.tag.filter(
            (item) => item.tag !== tag,
          );
        }
      }
      /**
       * @description: 左侧标签操作：添加标签
       * @param {*} value
       * @return {*}
       */
      const addTag=async(value)=> {
        const data = {
          assets: [
            {
              category: category.value,
              assetId: id.value,
            },
          ],
          tags: [value],
        };
  
        const { code } = await store.dispatch('api/appendTagsV2', data);
  
        if (code === 0) {
          basicInfo.value.tag.push({
            tag: value,
            modifiable: true,
          });
        }
      }
      /**
       * @description: 获取分析评估结果
       * @return {*}
       */
      const getAnalysis=async()=> {
        const params = {    
            businessType: category.value,
            id: id.value,
        };
  
        const [response1, response2, response3] = await Promise.all([
          store.dispatch('detail/getAnalysisExposureRisk', params),
          store.dispatch('detail/getAnalysisExposureLevel', params),
          store.dispatch('detail/getAnalysisExposureVulnerability', params),
        ]);
  
        const exposureStatus = store.state.enum.exposureStatus;
        const attackStatus = store.state.enum.attackStatus;
        const updateBlockList = (index, levelData, statusList, imgName) => {
  
          if (levelData.code === 0) {
            const level = statusList.find(
              (el) => el.type === levelData.data.level,
            );
  
            let img = require(`~/static/images/risk-primary.png`);
            let tag = 'primary';
            let tip = '--';
            if (level?.tag) {
              img = require(`~/static/images/${imgName}${level.tag}.png`);
              tag = level.tag;
              tip = level.value;
            }
  
            blockList.value[index] = {
              ...blockList.value[index],
              img: img,
              tip: tip,
              tag: tag,
              list:
                levelData.data.items.map((item) => {
                  return { ...item, icon: item.existBln };
                }) || [],
            };
          }
        };
  
        updateBlockList(0, response1, exposureStatus, 'risk-');
        updateBlockList(1, response2, exposureStatus, 'level-');
        updateBlockList(2, response3, attackStatus, 'Attack-');
      }
      /**
       * @description: 提示误报
       * @return {*}
       */
     const showPrompt=()=> {
        ElMessageBox.confirm('确认此资产为误报吗？')
          .then(async () => {
            const { code, message = '标记成功！' } = await store.dispatch(
              'expose/markAsset',
              {
                assetType: category.value,
                assetId: id.value,
                misinformation: 1,
              },
            );
  
            if (code === 0) {
              // this.$sdk.toast.success(message)
            }
          })
          .catch((_) => {
          });
      } 
      return {
        category,
        detialInfo,
        basicInfo,
        changeList,
        AssetChanged,
        blockList,
        id,
        getDetialById,
        getLeftBaseInfo,
        getAssetChanged,
        delTag,
        addTag,
        getAnalysis,
        showPrompt,
      };
}


  